package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ContactAdspter adapter;
    private RecyclerView rv_contact;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init(){
        rv_contact = findViewById(R.id.rv_contact);
        rv_contact.setLayoutManager(new LinearLayoutManager(this));
        getPermissions();
    }
    String[] pernissionList;
    public void getPermissions()
    {
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
        {
            pernissionList = new String[]{"android.permission.READ_CONTACTS"};
            ArrayList<String> list = new ArrayList<>();

            for (int i=0;i<pernissionList.length;i++)
            {
                if (ActivityCompat.checkSelfPermission(this,pernissionList[i])!= PackageManager.PERMISSION_GRANTED)
                {
                    list.add(pernissionList[i]);
                }
            }
            if (list.size()>0)
            {
                ActivityCompat.requestPermissions(this,list.toArray(new String[list.size()]),1);
            }else {
                setData();
            }
        }else {
            setData();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==1)
        {
            for (int i=0;i<permissions.length;i++)
            {
                if (permissions[i].equals("android.permission.READ_CONTACTS")&&grantResults[i]==PackageManager.PERMISSION_GRANTED)
                {
                    Toast.makeText(this,"所有权限已授权",Toast.LENGTH_LONG).show();
                }
                else
                {
                    Toast.makeText(this,"所有权限未授权",Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    private void setData(){
        List<ContacInfo> contacInfos = getContacts();
        adapter = new ContactAdspter(MainActivity.this, contacInfos);
        rv_contact.setAdapter(adapter);
    }
    @SuppressLint("Range")
    public List<ContacInfo> getContacts(){
        List<ContacInfo> contacInfos = new ArrayList<>();
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
        if (contacInfos!=null)contacInfos.clear();
        while (cursor.moveToNext()){
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts
                    ._ID));
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            int isHas = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
            if (isHas>0){
                Cursor cursor1=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null);
                while (cursor1.moveToNext()){
                    ContacInfo info = new ContacInfo();
                    info.setContactName(name);
                    String number = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).trim();
                    number = number.replace("","").replace("-","").replace("(","").replace(")","");
                    info.setPhoneNumber(number);
                    contacInfos.add(info);
                }
                cursor.close();
            }
        }
        cursor.close();
        return contacInfos;
    }
}